package com.example.yichuangyun.ui.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.FragmentDetailsFollowRecordBinding;
import com.example.yichuangyun.dialog.MyDialog;
import com.example.yichuangyun.http.HttpRespond;
import com.example.yichuangyun.http.MyObserver;
import com.example.yichuangyun.http.RetrofitHelper;
import com.example.yichuangyun.model.business.CallRecordDataModel;
import com.example.yichuangyun.mvvm.base.BaseFragment;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.ui.adapter.CallRecordAdapter;
import com.example.yichuangyun.utils.RequestUtils;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 商机详情-通话记录
 */
public class DetailsCallRecordFragment extends BaseFragment<FragmentDetailsFollowRecordBinding, BaseViewModel> {

    private int page = 1;
    private final static String BUSINESS_ID = "businessId";
    private final static String BUSINESS_CODE = "businessCode";
    private final static String BUSINESS_NAME = "businessName";
    private String businessId;
    private String businessCode;
    private List<CallRecordDataModel.CallListModel> callRecordData = new ArrayList<>();
    private CallRecordAdapter callRecordAdapter;
    /**
     * 拨打的电话号码
     */
    private String mTelNumber;
    private String clientName;
    private MyDialog myDialog;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void initData() {
        initDialog();
    }

    @Override
    public void lazyLoadData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            businessId = bundle.getString(BUSINESS_ID);
            businessCode = bundle.getString(BUSINESS_CODE);
            clientName = bundle.getString(BUSINESS_NAME);
        }
        checkButtonOrTab();
    }

    /**
     * 通话记录
     */
    private void getCallRecord() {
        Map<String, Object> map = new HashMap<>();
        map.put("current", page);
        map.put("size", Constant.pageSize);
        map.put("chanceId", businessId);
        map.put("isSync", 1);
        map.put("sjCode", businessCode);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(map));
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().getBusinessCallLog(requestBody), new MyObserver<HttpRespond<CallRecordDataModel>>() {
            @Override
            protected void onSuccess(HttpRespond<CallRecordDataModel> callRecordDataModelHttpRespond) {
                if (Constant.SUCCESS == callRecordDataModelHttpRespond.code) {
                    if (callRecordDataModelHttpRespond.data != null) {
                        List<CallRecordDataModel.CallListModel> list = callRecordDataModelHttpRespond.data.getRecords();
                        if (list != null && list.isEmpty()) {
                            binding.smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }
                        binding.smartRefreshLayout.setEnableLoadMore(callRecordDataModelHttpRespond.data.getTotal() > 10);
                        if (page == 1) {
                            callRecordData = list;
                            callRecordAdapter.onRefresh(list);
                            binding.smartRefreshLayout.finishRefresh();
                        } else {
                            callRecordAdapter.addData(list);
                            binding.smartRefreshLayout.finishLoadMore();
                        }
                    }
                }
            }
        });
    }

    @Override
    public void initCtrl() {
        callRecordAdapter = new CallRecordAdapter(mContext, callRecordData, 1);
        binding.recyclerView.setAdapter(callRecordAdapter);
    }

    @Override
    protected void initClick() {
        callRecordAdapter.setOnCallListener(number -> {
            mTelNumber = number;
            RequestUtils.checkCallNum(mContext, mTelNumber, myDialog, 0, true);
        });

        binding.smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getCallRecord();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getCallRecord();
            }
        });
    }

    private void initDialog() {
        myDialog = new MyDialog(getActivity()) {
            @Override
            public void leftClick() {
                RequestUtils.callNum(mContext, mTelNumber);
                Constant.pushBusinessId = businessId;
                RequestUtils.addFollow(businessId, clientName, mTelNumber);
            }

            @Override
            public void rightClick() {
                RequestUtils.callbackOutbound(mContext, mTelNumber, businessId, clientName);
            }
        };
    }

    public static DetailsCallRecordFragment newInstance(String businessId, String businessCode, String businessName) {
        Bundle bundle = new Bundle();
        bundle.putString(BUSINESS_ID, businessId);
        bundle.putString(BUSINESS_CODE, businessCode);
        bundle.putString(BUSINESS_NAME, businessName);
        DetailsCallRecordFragment callRecordFragment = new DetailsCallRecordFragment();
        callRecordFragment.setArguments(bundle);
        return callRecordFragment;
    }

    /**
     * 检测是否有按钮权限
     */
    private void checkButtonOrTab() {
        JSONObject requestDate = new JSONObject();
        try {
            // 1、跟进记录 2、通话记录 3、订单记录 4、添加跟进 5、编辑信息 6、设置共享人 7、丢单 8、商机详情
            requestDate.put("type", 2);
            // 商机类型  0、我创建的 1、部门创建的 2、我跟进的 3、部门跟进的 4、共享给我的 5、共享给部门的
            requestDate.put("businessType", Constant.businessType);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), requestDate.toString());
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().checkButtonOrTab(requestBody), new MyObserver<HttpRespond<Boolean>>() {
            @Override
            protected void onSuccess(HttpRespond<Boolean> booleanHttpRespond) {
                if (Constant.SUCCESS == booleanHttpRespond.code) {
                    if (booleanHttpRespond.data) {
                        getCallRecord();
                    }
                }
            }
        });
    }
}
